package cn.jdy.plugins.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.authority.entity.UserRole;
import cn.jdy.authority.service.UserRoleService;
import cn.jdy.authority.service.UserService;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.ChatData;
import cn.jdy.plugins.entity.Live;

/**
 * @desc   交流区内容操作
 * @author ningqian
 * @date   2017-3-23
 */
@Service
public class ChatDataService extends BaseDao<ChatData>{
	
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserService userService;
	@Autowired
	private LiveService liveService;
	
	/**
	 * 按条件查询
	 * @param Kindergarten
	 * @return
	 */
	public List<ChatData> query(ChatData chat, String... date) {
		if(null == chat) return super.findAll();
		if(chat.getiDisplayLength() > 0){
		  return super.find(createFilter(chat, date),chat.getiDisplayStart(),chat.getiDisplayLength());
		}
		return super.find(createFilter(chat, date));
	}

	public Long count(ChatData chat, String... date) {
		return super.count(createFilter(chat, date));
	}
	
	/**
	 * 拼装条件(直播id、创建时间)
	 * @param Kindergarten
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(ChatData chat, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != chat.getLiveId()){
		  dc.add(Restrictions.eq("liveId", chat.getLiveId()));
	  }
	  
	  if(StringUtil.isNotEmpty(chat.getChatType())){
		  dc.add(Restrictions.eq("chatType", chat.getChatType()));
	  }
	  if(StringUtil.isNotEmpty(chat.getChatContent())){
		  dc.add(Restrictions.like("chatContent", chat.getChatContent(),MatchMode.ANYWHERE));
	  }
	  if(null !=chat.getCahtRobotId()){
		  dc.add(Restrictions.eq("cahtRobotId", chat.getCahtRobotId()));
	  }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
//查询当前时间之前的数据
	public  List<ChatData> findChatRobot(Long liveId, String dsdata) {
		List<ChatData> list = new ArrayList<ChatData>();
		String allSql=" ";
	
		if(liveId!=null ){
			allSql =allSql +" select * FROM chatdata where liveId="+liveId+" ";
		}
		
		if(dsdata!="" ){
			allSql  += " and createTime<='"+dsdata+"' and isok='0'";
		}
		
		if(""!= allSql){
			SQLQuery query = super.em().createSQLQuery(allSql.toString()).addEntity(ChatData.class);
			list = query.list();
		}
		return list;	
		
	}
	
	
	
	
	
		
}
